package com.eveinfo.display.certificates;

import java.util.Set;
import java.util.Map.Entry;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.eveinfo.EveInfo;
import com.eveinfo.api.character.sheet.ApiSheet;
import com.eveinfo.api.utils.certificates.ApiCertificate;
import com.eveinfo.api.utils.certificates.ApiClass;
import com.eveinfo.display.CategoriesView;
import com.eveinfo.display.SheetView;
import com.eveinfo.display.planner.PlannerManager;
import com.eveinfo.display.planner.PlannerTools;
import com.eveinfo.tools.ActivityLauncher;

public class CertificatesAdapter extends BaseAdapter {

	private ApiClass apiClass;
	private final Context context;
	private final ApiSheet sheet;
	private final TextView descriptionTV;
	private static Integer[] unfold;

	public CertificatesAdapter(final Context context, ListView listview, ApiClass apiClass, TextView descriptionTV,
			ApiSheet sheet) {
		this.sheet = sheet;
		this.context = context;
		this.apiClass = apiClass;
		this.descriptionTV = descriptionTV;
		int size = apiClass.getCertificates().size();
		unfold = new Integer[size];
		for (int i = 0; i < size; i++) {
			unfold[i] = 0;
		}
		listview.setAdapter(this);
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public int getCount() {
		int total = 0;
		int i = 0;
		for (ApiCertificate section : apiClass.getCertificates()) {
			if (unfold[i] == 1) {
				total += section.getRequiredCertificates().size() + section.getRequiredSkills().size() + 1;
			} else {
				total++;
			}
			i++;
		}
		return total;
	}

	public boolean areAllItemsSelectable() {
		return false;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		int sectionnum = 0;
		int pos = position;
		int i = 0;
		for (ApiCertificate section : apiClass.getCertificates()) {
			final int y = i;
			int sectionSize = 1;

			if (unfold[i] == 1) {
				sectionSize += section.getRequiredCertificates().size() + section.getRequiredSkills().size();
			}

			CertificateViewRenderer renderer = new CertificateViewRenderer(context);
			if (pos == 0) {
				final ApiCertificate certificateF = section;
				renderer.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (unfold[y] == 0) {
							unfold[y] = 1;
						} else {
							unfold[y] = 0;
						}
						descriptionTV.setText(certificateF.getDescription());
						notifyDataSetChanged();
					}
				});
				renderer.setOnLongClickListener(new OnLongClickListener() {
					@Override
					public boolean onLongClick(View v) {
						PlannerManager plannerManager = SheetView.getPlannerManager();
						Dialog dialog = PlannerTools.createDialogAddCertificate(context, plannerManager, certificateF);
						if (dialog == null) {
							AlertDialog.Builder builder = new AlertDialog.Builder(context);
							builder.setMessage("No planner");
							AlertDialog alert = builder.create();
							alert.show();
							return true;
						}else{
							dialog.show();
						}
						return true;
					}
				});
				renderer.displayCertificateHeader(section, sheet);
				return renderer;
			}
			if (pos < sectionSize) {
				int typeID = -1;
				int level = -1;
				boolean isSkill = true;
				isSkill = false;

				Set<Entry<Integer, Integer>> set = section.getRequiredCertificates().entrySet();
				if (pos > set.size()) {
					pos -= set.size();
					set = section.getRequiredSkills().entrySet();
					isSkill = true;
				}
				int j = 0;
				for (Entry<Integer, Integer> e : set) {
					if ((pos - 1) == j) {
						typeID = e.getKey();
						level = e.getValue();
						break;
					}
					j++;
				}
				final int typeIDF = typeID;
				final boolean isSkillF = isSkill;

				renderer.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (isSkillF) {
							ActivityLauncher.displaySkill(context, typeIDF, sheet.getCharacterID());
						} else {

						}
					}
				});
				final int levelF = level;
				renderer.setOnLongClickListener(new OnLongClickListener() {
					@Override
					public boolean onLongClick(View v) {
						PlannerManager plannerManager = SheetView.getPlannerManager();
						Dialog dialog = null;
						if (isSkillF) {
							dialog = PlannerTools.createDialogAddSkill(context, plannerManager, EveInfo.getData()
									.getSkills().getSkill(typeIDF), levelF);
						} else {
							ApiCertificate certificate = CategoriesView.certificates.getCertificates().getCertificate(
									typeIDF);
							dialog = PlannerTools.createDialogAddCertificate(context, plannerManager, certificate);
						}
						if (dialog == null) {
							AlertDialog.Builder builder = new AlertDialog.Builder(context);
							builder.setMessage("No planner");
							AlertDialog alert = builder.create();
							alert.show();
							return true;
						}
						dialog.show();
						return true;
					}
				});

				if (isSkill) {
					renderer.displaySkill(typeID, level, sheet);
				} else {
					ApiCertificate certificate = CategoriesView.certificates.getCertificates().getCertificate(typeID);
					renderer.displayCertificate(certificate, sheet);
				}
				return renderer;
			}

			pos -= sectionSize;
			sectionnum++;
			i++;
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
}
